package com.feihong.txgw.shopcar.viewholder;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.Holder.BaseViewHolder;
import com.feihong.txgw.model.greendao.ShopCarGoodsModel;
import com.feihong.txgw.ui.activity.base.BaseActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * @name txgw_app
 * @class name：com.feihong.txgw.shopcar.viewholder
 * @class describe:
 * @author wangziheng
 * @time 2017/9/15 17:50
 * @change
 * @chang time
 * @class describe
 */
public class ShopCarFailureViewHolder extends BaseViewHolder{
    private TextView mItemGoodsName,mItemGoodsDescribe;
    private ImageView mItemGoodsImg;

    private ImageView mItemGoodsSelect;
    private TextView mItemGoodsPrice;  // 商品名称 价格
    private TextView mItemGoodsPriceRemainder; // 价格后缀
    private TextView mFailureTv;
    private LinearLayout mGoodsCountChangeLayout;
    private BaseActivity mContext;
    public ShopCarFailureViewHolder(BaseActivity context,View itemView) {
        super(itemView);
        mContext = context;
        mItemGoodsName = (TextView) itemView.findViewById(R.id.shopCar_Goods_name);
        mItemGoodsPrice = (TextView) itemView.findViewById(R.id.shopCar_Goods_price);
        mItemGoodsSelect = (ImageView) itemView.findViewById(R.id.shopCar_item_select_status);
        mItemGoodsPriceRemainder = (TextView) itemView.findViewById(R.id.shopCar_Goods_price_remainder);
        mItemGoodsPriceRemainder.setVisibility(View.GONE);
        mItemGoodsDescribe = (TextView) itemView.findViewById(R.id.shopcar_goods_describe);
        mItemGoodsImg = (ImageView) itemView.findViewById(R.id.shopCar_item_Goods_img);
        mFailureTv = (TextView) itemView.findViewById(R.id.shopcar_item_goods_failure_warn);
        mItemGoodsSelect.setVisibility(View.GONE);
        mFailureTv.setVisibility(View.VISIBLE);
        mGoodsCountChangeLayout = (LinearLayout) itemView.findViewById(R.id.shopcar_item_count_change_layout);
        mGoodsCountChangeLayout.setVisibility(View.GONE);
    }

    public void initView(ShopCarGoodsModel failureGoods){
        mItemGoodsName.setText(failureGoods.getGoodsName());
        StringBuffer stringBuffer = new StringBuffer();
        try {
            if(failureGoods.getGoodsSpec() != null){
                JSONArray jsonArray = new JSONArray(failureGoods.getGoodsSpec());
                    for(int i = 0 ; i < jsonArray.length(); i ++){
                        JSONObject jsonObject = jsonArray.optJSONObject(i);
                        stringBuffer.append(jsonObject.optString("name")+":"+jsonObject.optString("value")+"   ");
                    }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        mItemGoodsDescribe.setText(stringBuffer.toString());
        mItemGoodsPrice.setText("¥ "+failureGoods.getGoodsPrice());
        Glide.with(mContext).load(failureGoods.getGoodsPic()).into(mItemGoodsImg);
    }
}
